package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckitemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("checkitem")
@Slf4j
public class CheckItemController {

    @DubboReference
    private CheckitemService checkitemService;

    /**
     * @Author: 15041
     * @Date: 18:23 2021/1/18
     * @Parms: [queryPageBean]
     * @ReturnType: com.itheima.health.entity.Result
     *Description: 分页查询
    */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')") //设置权限
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean){
        log.info("检查项-检查项controller层分页查询方法中传递过来的数据是："+queryPageBean);
        PageResult pageResult = checkitemService.findPage(queryPageBean);
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS,pageResult);
    }

    /**
     * @Author: 15041
     * @Date: 18:23 2021/1/18
     * @Parms: [checkItem]
     * @ReturnType: com.itheima.health.entity.Result
     *Description: 新增检查组
    */
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')") //设置权限
    @PostMapping("add")
    @Transactional(rollbackFor = Exception.class)//添加事务 如果出现异常回滚
    public Result add(@RequestBody CheckItem checkItem){
        log.info("检查项-检查项controller层新增方法中传递过来的数据是："+checkItem);
        checkitemService.add(checkItem);
        return new Result(true,MessageConst.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * @Author: 15041
     * @Date: 18:24 2021/1/18
     * @Parms: [id]
     * @ReturnType: com.itheima.health.entity.Result
     *Description: 删除检查组
    */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')") //设置权限
    @PostMapping("delete")
    @Transactional(rollbackFor = Exception.class)//添加事务 如果出现异常回滚
    public Result delete(Integer id){
        log.info("检查项-检查项controller层删除方法中前端传过来的参数是："+id);
        checkitemService.delete(id);
        return new Result(true,MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * @Author: 15041
     * @Date: 18:24 2021/1/18
     * @Parms: [id]
     * @ReturnType: com.itheima.health.entity.Result
     *Description: 根据id查询检查组
    */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')") //设置权限
    @GetMapping("findById")
    public Result findById(Integer id){
        log.info("检查项-检查项controller层根据id查询数据回显的方法中前端传过来的参数是："+id);
        CheckItem checkItem = checkitemService.findById(id);
        log.info("查询出来的数据是："+checkItem);
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItem);
    }

    /**
     * @Author: 15041
     * @Date: 18:24 2021/1/18
     * @Parms: [checkItem]
     * @ReturnType: com.itheima.health.entity.Result
     *Description: 修改检查组
    */
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')") //设置权限
    @PostMapping("update")
    @Transactional(rollbackFor = Exception.class)//添加事务 如果出现异常回滚
    public Result update(@RequestBody CheckItem checkItem){
        log.info("检查项-检查项controller层修改方法中前端传过来的参数是："+checkItem);
        checkitemService.update(checkItem);
        return new Result(true,MessageConst.EDIT_CHECKITEM_SUCCESS);
    }

}
